package com.hxkcloud.patient.aerotel.notification;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.aerotel.AerotelActivity;


/**
 * Created by daipengfei on 16/3/9.
 */
public class HDCNotification {

    private final static String NOTIFICATION_TAG = "HDCNotification";

    public static HDCNotification hdcnotification = null;

    public static HDCNotification getHDCNotification() {
        return hdcnotification;
    }

    private Context context;
    private Notification notification = null;
    private NotificationCompat.Builder builder = null;

    public Notification getNotification() {
        return notification;
    }

    public HDCNotification(Context context) {
        this.context = context;
        builder = new NotificationCompat.Builder(context);
        notify("服务已启动");
    }

    public void notify(String content) {

        builder.setDefaults(Notification.DEFAULT_LIGHTS)

                // Set required fields, including the small icon, the
                // notification title, and text.
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("心电监测")
                .setContentText(content)
                .setAutoCancel(false);

        PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, AerotelActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pi);

        notification = builder.build();
        notification.flags |= Notification.FLAG_NO_CLEAR;
        notify(context, notification);
    }

    public void notify(String text, int progress) {
        if (progress > 0 && progress <= 100) {
            builder.setProgress(100, progress, false);
            builder.setContentText(text);
            notification = builder.build();
            notification.flags |= Notification.FLAG_NO_CLEAR;
            notify(context, notification);
        } else {
            builder = new NotificationCompat.Builder(context);
            notify(text);
        }

    }

    @TargetApi(Build.VERSION_CODES.ECLAIR)
    private void notify(final Context context, final Notification notification) {
        final NotificationManager nm = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
            nm.notify(NOTIFICATION_TAG, 0, notification);
        } else {
            nm.notify(NOTIFICATION_TAG.hashCode(), notification);
        }
    }

    @TargetApi(Build.VERSION_CODES.ECLAIR)
    public void cancel(final Context context) {
        final NotificationManager nm = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
            nm.cancel(NOTIFICATION_TAG, 0);
        } else {
            nm.cancel(NOTIFICATION_TAG.hashCode());
        }
    }

    public void notify(int progress) {
        notify("正在接收心电图..." + progress + "%", progress);
    }
}
